<?php
class Feeds extends Public_Controller
{
	
	public function __construct()
	{
		parent::__construct();
	}
	
	public function index()
	{
		if(is_login('Administrator'))
		{
			$data['feeds'] = new Feed();
			$data['feeds']->order_by('id','desc')->get_page();
			$this->template->build('index',$data);
		}
		else
		{
			set_notify('error','คุณไม่สามารถเข้าถึงข้อมูลในส่วนนี้ค่ะ');
			redirect('home');
		}
	}
	
	public function form($id = NULL)
	{
		if(is_login('Administrator'))
		{
			$data['feed'] = new Feed($id);
			$this->template->build('form',$data);
		}
		else
		{
			set_notify('error','คุณไม่สามารถเข้าถึงข้อมูลในส่วนนี้ค่ะ');
			redirect('home');
		}
	}
	
	public function save($id = NULL)
	{
		if(is_login('Administrator'))
		{
			$feed = new Feed($id);
			$feed->from_array($_POST);
			$feed->save();
			set_notify('success','บันทึกข้อมูลเรียบร้อยแล้วค่ะ');
			redirect('feeds');
		}
		else
		{
			set_notify('error','คุณไม่สามารถเข้าถึงข้อมูลในส่วนนี้ค่ะ');
			redirect('home');
		}
	}
	
	public function delete($id)
	{
		if(is_login('Administrator'))
		{
			$feed = new Feed($id);
			$feed->delete();
			set_notify('success','ลบข้อมูลเรียนร้อยแล้วค่ะ');
			redirect('feeds');
		}
		else
		{
			set_notify('error','คุณไม่สามารถเข้าถึงข้อมูลในส่วนนี้ค่ะ');
			redirect('home');
		}
	}
	
	public function lists($feed_id,$id=NULL)
	{
		$data['feed'] = new Feed($feed_id);
		if($id)
		{
			$data['content'] = new $data['feed']->model;
			$data['content']->get_by_id($id);
			$this->template->build('view',$data);
		}
		else
		{
			$data['contents'] = new $data['feed']->model;
			$data['contents']->where('title !=','')->order_by('id','desc')->get_page(15);
			$this->template->build('lists',$data);
		}
	}
	
	public function inc_index()
	{
		$data['feeds'] = new Feed;
		$data['feeds']->order_by('name','asc')->get();
		$this->load->view('inc_index',$data);
	}
	
	public function js($feed_id,$limit = 20)
	{
		$data['feed'] = new Feed($feed_id);
		$data['contents'] = new $data['feed']->model;
		echo $data['contents']->select('id,title,created')->where('title !=','')->order_by('id','desc')->get_page($limit)->all_to_json();
		
	}
	
	public function js_view($id)
	{
		$data['content'] = new Pingbook;
		echo $data['content']->get_by_id($id)->to_json();
	}
	
	public function view($feed_id,$id)
	{
		$data['feed'] = new Feed($feed_id);
		if($id)
		{
			$data['content'] = new $data['feed']->model;
			$data['content']->get_by_id($id);
			$this->load->view('mobile_view',$data);
		}
		
	}
	
}
?>